import 'package:flutter/material.dart';

import '../base/axis_entity.dart';
import '../line/added_line.dart';

/// 柱状图柱形数据
///
/// Created by wanggaowan on 2023/11/30 13:11
class BarEntity extends AxisEntity {
  BarEntity(
      {super.xIndex,
      super.value,
      super.isDraw,
      super.data,
      this.drawLabel = false,
      this.label,
      this.labelTextSize = 12,
      this.labelColor = Colors.black,
      this.isLimitedByBarWidth = true,
      this.labelOffset = 4,
      this.labelFixTop = false,
      this.barColors = const [Colors.grey],
      this.bgColor = Colors.transparent,
      this.widthPercent = 0.8})
      : assert(widthPercent >= 0 && widthPercent <= 1,
            "widthPercent value must be in [0,1]"),
        assert(barColors.isNotEmpty,
            "barColors at least one color value must be set");

  /// 是否绘制标签
  final bool drawLabel;

  /// 标签值
  final String? label;

  /// 标签是否受柱状宽度约束
  // final bool isConstrainedByWidth;

  /// 标签字体大小
  final double labelTextSize;

  /// 标签字体颜色
  final Color labelColor;

  /// 标签文本绘制宽度是否受限柱子宽度
  final bool isLimitedByBarWidth;

  /// 标签与柱形图的间距
  final double labelOffset;

  /// 柱状图标签是否固定在图表顶部，不随柱状图实际高度而变动
  final bool labelFixTop;

  /// 柱形的颜色集合，实现渐变色。该方法在绘制过程多次调用，因此不要每次调用都创建新对象
  final List<Color> barColors;

  /// 柱形背景颜色，如果此值不是透明色，则始终绘制完整的背景柱形图
  final Color bgColor;

  /// 柱形宽度占可绘制宽度的百分比,如果[BarGroup]中设置多个[BarEntity],请确保所有[BarEntity.widthPercent]之和不超过1，
  /// 否则将出现部分柱形图绘制不全的问题
  final double widthPercent;
}

/// 柱状图绘制方向
enum BarGravity {
  /// 从网格起始位置往结束位置绘制
  start,

  /// 从网格中间向两边绘制
  center,

  /// 从网格结束位置往起始位置绘制
  end
}

/// 柱状图分组数据
class BarGroupEntity {
  const BarGroupEntity(
      {required this.values,
      this.barOffset = 2,
      this.gravity = BarGravity.center});

  final List<BarEntity> values;

  /// 柱状图之间的间距
  final double barOffset;
  final BarGravity gravity;
}

/// 柱状图数据
class BarData {
  BarData(
      {List<BarEntity>? values,
      List<AddedLine>? addedLines,
      BarGravity gravity = BarGravity.center})
      : this.group(
            groups: values
                ?.map((e) => BarGroupEntity(values: [e], gravity: gravity))
                .toList(),
            addedLines: addedLines);

  /// x轴每个节点柱形图有多个
  const BarData.group({this.groups, this.addedLines});

  /// 柱形分组数据
  final List<BarGroupEntity>? groups;

  /// 附加线数据
  final List<AddedLine>? addedLines;
}
